package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/19.<br><br>
 *
 * 检测大写字母
 */
public class Topic520 {

    public static void main(String[] args) {
        Topic520 instance = new Topic520();

        System.out.println(instance.detectCapitalUse("USA"));
        System.out.println(instance.detectCapitalUse("leetcode"));
        System.out.println(instance.detectCapitalUse("Google"));
        System.out.println(instance.detectCapitalUse("GooGle"));
        System.out.println(instance.detectCapitalUse("mL"));
    }

    public boolean detectCapitalUse(String word) {
        int counter = 0;
        for(char ch : word.toCharArray()) {
            if('Z' - ch >= 0) {
                counter++;
            }
        }
        return counter == word.length() || counter == 0 || (counter == 1 && word.charAt(0) <= 'Z');
    }

    public boolean detectCapitalUse2(String word) {
        char[] array = word.toCharArray();

        boolean up = false;
        for(char ch : array) {
            if(ch >= 'A' && ch <= 'Z') {
                up = true;
            }
            else {
                up = false;
                break;
            }
        }

        if(up) {
            return true;
        }

        for(int i = 1; i < array.length; i++) {
            if(array[i] < 'a' || array[i] > 'z') {
                return false;
            }
        }

        return true;
    }
}
